package se.jocke.nbrs.ui.window;

import com.google.common.eventbus.Subscribe;
import javax.swing.ActionMap;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.WindowManager;
import se.jocke.nbrs.service.EventBus;
import se.jocke.nbrs.service.RestServiceManager;
import se.jocke.nbrs.service.event.RestServiceGroupChanged;
import se.jocke.nbrs.ui.node.RestServiceRootNode;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//se.jocke.nbrs.ui.window//RestService//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = RestServiceTreeTopComponent.PREF_ID,
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "explorer", openAtStartup = false)
@ActionID(category = "Window", id = "se.jocke.nbrs.ui.window.RestServiceTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_RestServiceAction",
        preferredID = RestServiceTreeTopComponent.PREF_ID
)
@Messages({
    "CTL_RestServiceAction=Rest Services",
    "CTL_RestServiceTopComponent=Rest Services",
    "HINT_RestServiceTopComponent=This is a RestService window"
})
public final class RestServiceTreeTopComponent extends TopComponent implements ExplorerManager.Provider {

    public static final String PREF_ID = "RestServiceTopComponent";

    private final ExplorerManager mgr = new ExplorerManager();

    private Lookup lookup;

    public RestServiceTreeTopComponent() {
        initComponents();
        setName(Bundle.CTL_RestServiceTopComponent());
        setToolTipText(Bundle.HINT_RestServiceTopComponent());
        ActionMap actionMap = getActionMap();
        lookup = ExplorerUtils.createLookup(mgr, actionMap);
        associateLookup(lookup);
        mgr.setRootContext(new RestServiceRootNode());
        BeanTreeView beanTreeView = (BeanTreeView) restBeanTree;
        beanTreeView.setRootVisible(false);
       
    }

    @Override
    public Lookup getLookup() {
        return lookup;
    }

    @Subscribe
    public void onModelChange(RestServiceGroupChanged evt) {
        mgr.setRootContext(new RestServiceRootNode());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        restBeanTree = new BeanTreeView();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(restBeanTree, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(restBeanTree, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane restBeanTree;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        WindowManager.getDefault().findTopComponent(RestServiceEditorTopComponent.PREF_ID).open();
        Lookup.getDefault().lookup(EventBus.class).register(this);
    }

    @Override
    public void componentClosed() {
        WindowManager.getDefault().findTopComponent(RestServiceEditorTopComponent.PREF_ID).close();
        Lookup.getDefault().lookup(EventBus.class).unregister(this);
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return mgr;
    }

}
